package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: HouseController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/26 11:30
 * @SINCE 17.0.7
 * @DESCRIPTION: HouseController
 */
@Slf4j
@RestController
@RequestMapping("/house")
public class HouseController extends BaseController {

    @Reference
    private HouseService houseService;

    @Reference
    private CommunityService communityService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private UserFollowService userFollowService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<HouseVo>> findHouseList(@PathVariable("pageNum") Integer pageNum,
                                @PathVariable("pageSize") Integer pageSize,
                                @RequestBody HouseQueryVo houseQueryVo) {
        log.debug("houseQueryVo = {}", houseQueryVo);
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum,pageSize,houseQueryVo);
        return Result.ok(pageInfo);
    }

    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id, HttpSession session) {
        if (null == id) {
            return Result.fail();
        }
        House house = houseService.getDictNameById(id);
        Community community = communityService.getDictNameById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(house.getId());
        List<HouseImage> houseImage1List = houseImageService.findListByHouseIdAndType(house.getId(), 1);

        Map<String, Object> jsonMap = new HashMap<>(6);
        jsonMap.put("house", house);
        jsonMap.put("community", community);
        jsonMap.put("houseBrokerList", houseBrokerList);
        jsonMap.put("houseImage1List", houseImage1List);

        Boolean isFollow = false;

        Object userInfo = session.getAttribute("USER");
        if (null == userInfo) {
            isFollow = false;
        } else {
            Long userId = ((UserInfo) userInfo).getId();
            isFollow = userFollowService.isFollowed(userId, id);
        }
        jsonMap.put("isFollow", isFollow);
        return Result.ok(jsonMap);
    }
}
